package fhjoanneum.itm11.antesk.simplehack;
import java.io.*;
import java.util.*;

public class Config
{
	// Members -----------------------------------------------------------
	private final String str_file_name;
  private char map[][];
  private String[] file_contents;
  private String line; // temporary storage for read lines
  private int height;
	private int width;
	private Properties prop = new Properties();

  // Constructor -------------------------------------------------------
  Config()
  {
  	str_file_name = null; // whyever...
  }
  
  // Copy Constructor --------------------------------------------------
  Config(String filename)
  {
  	str_file_name = filename;
  }
  
  // -------------------------------------------------------------------
  public String[] getFileContents(){return file_contents;}
	public void setFileContents(String[] file_contents){
  	this.file_contents = file_contents;}
  public char[][] getMap(){return map;}
	public void setMap(char[][] map){this.map = map;}
	public int getHeight(){return height;}
	public void setHeight(int height){this.height = height;}
	public int getWidth(){return width;}
	public void setWidth(int width){this.width = width;}

	/**
	 * Returns the number of lines of the text file specified in 
	 * str_file_name as an Integer. If an exception is caught, it returns
	 * a message accordingly.
	 * 
	 * @return integer of line numbers.
	 */
  public int readLineNumber()
  {
  	try
    {
	    LineNumberReader lnr = 
	    		new LineNumberReader(new FileReader(str_file_name));
	    lnr.skip(Long.MAX_VALUE);
	    
	    return (lnr.getLineNumber() + 1);
    }
    catch (Exception e)
    {
	    e.printStackTrace(); // work needed!
    }
  	return -1; // error return
  }  

  /**
   * Reads the file specified in str_file_name and stores the contents
   * in a String array named file_contents. Prints a message if an error
   * is found.
   */
  public void readFile()
  {
  	// Check for positive line number
  	if(readLineNumber() == -1)
  		System.out.println("File error or corrupted.");
  	else
  		file_contents = new String[readLineNumber()];
  	
  	// Actual file reading happens
  	try
    {
	    BufferedReader br = new BufferedReader(new FileReader(str_file_name));
	    int counter = 0;
	    
	    while((line = br.readLine()) != null)
	    {
	    	file_contents[counter] = line;
	    	counter++;
	    }
	    
	    makeMap();
	    fillValues();
    }
    catch (Exception e)
    {
	    System.out.println(e.getMessage());
    }
  }
  
  /**
   * Fills the map[][] char array with values from the file_contents[]
   * String array.
   * 
   * This method reads the file_contents[] array char by char and fills
   * the two-dimensional array map[][] with the read values. Currently
   * only works on perfect rectangle maps, meaning every line has to
   * be of the exact same length.
   */
  private void makeMap()
  {
  	map = 
  		new char[getFileContents().length][getFileContents()[0].length()];
  	
  	for(int y = 0; y < getFileContents().length; y++){
  		for(int x = 0; x < getFileContents()[0].length(); x++){
  			if(!file_contents[y].isEmpty())
  			{
  				map[y][x] = file_contents[y].charAt(x);
  			}
  			else
  			{
  				map[y][x] = ' ';
  			}
  		}
  	}
  }
  
  // Fills the height and width members --------------------------------
  private void fillValues()
  {
  	height = getFileContents().length;
  	width = getFileContents()[0].length();
  }
  
  // Read the settings -------------------------------------------------
  public String readProperty(String property)
  {
  	try
    {
	    prop.load(new FileInputStream(str_file_name));
	    return prop.getProperty(property);
    }
    catch (Exception e)
    {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
    }
  	
  	return "Property not found.";
  }
  
  // -------------------------------------------------------------------
  public Vector<Integer> parsePositions(String value)
  {
  	Vector<Integer> positions = new Vector<Integer>();
  	
  	for (String current_value : value.split(" "))
  	{
  		try
  		{
  			positions.add(Integer.valueOf(current_value));
  		}
  		catch(Exception e)
  		{
  			
  		}
  	}
  	
  	return positions;
  }

}
